<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="money">
        <span>账户金额</span>
        <div>￥{{ mch.balance }}</div>
      </div>
    </page-tab-list>
    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.sort" @change="clearData">
          <a-radio-button value="1"> 全部 </a-radio-button>
          <a-radio-button value="2"> 增加 </a-radio-button>
          <a-radio-button value="3"> 减少 </a-radio-button>
        </a-radio-group>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :pagination="{
          total: this.totalCount,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
        :columns="columns"
        :data-source="list"
        :scroll="{ x: 'max-content' }"
      >
        <span slot="name" slot-scope="scope">
          <!-- {{ scope.info.info_name }} -->
          <div class="goods-info">
            <img class="goods-pic" :src="scope.info.avatar_url" alt="" />
            <div class="info-box">
              <a-tooltip placement="top">
                <template slot="title">
                  <span class="goods-name">{{ scope.info.info_name }} </span>
                </template>
                <div style="font-weight: bold; line-height: 25px; width: 160px" class="of_el">
                  {{ scope.info.info_name }}
                </div>
              </a-tooltip>
              <div style="color: rgb(140, 140, 140); width: 160px" class="of_el">
                {{ scope.info.tel }}
              </div>
            </div>
          </div>
        </span>
        <span slot="mobile" slot-scope="scope">
          {{ scope.info.tel }}
        </span>
        <span slot="type" slot-scope="text">
          <!-- <a-tag color="#87d068" v-if="text == 1">
            增加
          </a-tag>
          <a-tag color="#f50" v-else>
            减少
          </a-tag> -->

          <span class="tag" :style="text | orderStatus"></span>
          <span>
            {{ getStatus(text) }}
          </span>
        </span>
        <span slot="before_price" slot-scope="text"> ￥{{ text || 0 }} </span>
        <span slot="after_price" slot-scope="text"> ￥{{ text || 0 }} </span>
        <span slot="price" slot-scope="text, item">
          {{ getPrice(item) }}
        </span>
      </a-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { banlanceRecord } from '@/api/purchase/business'
import tabList from '@/views/saas/components/tabList.vue'
import { cyyUploadImg } from '@/components/CyyUi'
const columns = [
  {
    title: '用户昵称',
    scopedSlots: {
      customRender: 'name',
    },
    width: 180,
  },
  {
    title: '变动前余额',
    dataIndex: 'before_price',
    scopedSlots: {
      customRender: 'before_price',
    },
    sorter: (a, b) => a.before_price - b.before_price,
  },
  {
    title: '变动后余额',
    dataIndex: 'after_price',
    scopedSlots: {
      customRender: 'after_price',
    },
    sorter: (a, b) => a.after_price - b.after_price,
  },
  {
    title: '变动金额',
    scopedSlots: {
      customRender: 'price',
    },
  },
  {
    title: '状态',
    dataIndex: 'type',
    scopedSlots: {
      customRender: 'type',
    },
  },
  {
    title: '添加时间',
    dataIndex: 'addtime',
    scopedSlots: {
      customRender: 'addtime',
    },
  },
  {
    title: '详情',
    dataIndex: 'desc',
  },
]

export default {
  components: {
    tabList,
    cyyUploadImg,
  },

  filters: {
    orderStatus(val) {
      if (val == 0) {
        return 'background-color:#faad14;'
      }

      if (val == 1) {
        return 'background-color:#65c817;'
      }
    },
  },

  computed: {
    getStatus() {
      return (status) => {
        let str

        switch (status) {
          case '0':
            str = '减少'
            break

          case '1':
            str = '增加'
            break
        }

        return str
      }
    },

    getPrice() {
      return (item) => {
        let str
        
        if (item.type === '0') {
          str = `-￥${(item.before_price - item.after_price).toFixed(2)}`
        } else {
          str = `+￥${(item.after_price - item.before_price).toFixed(2)}`
        }

        return str
      }
    },
  },

  data() {
    return {
      tabList: [
        {
          key: 'purchaseGoodsList',
          name: '云仓选品库',
        },
        {
          key: 'goodsCloudList',
          name: '我的云仓',
        },
        {
          key: 'purchaseTransRecord',
          name: '云仓转单',
        },
        {
          key: 'businessPurchaseBalance',
          name: '我的账户',
        },
        {
          key: 'purchaseSetting',
          name: '云仓设置',
        },
        {
          key: 'purchaseGoodsUpdateLog',
          name: '商品更新记录',
        },
        // {
        //     key:'businessPurchaseRecharge',
        //     name:'充值记录'
        // }
      ],
      // 搜索参数
      queryParam: {
        page: 1,
        limit: 10,
        sort: '1',
      },
      selectedRowKeys: [],
      columns,
      list: [],
      totalCount: 0,
      mch: {
        balance: '0.00',
      },
    }
  },

  created() {
    this.getList()
  },

  methods: {
    clearData() {
      this.queryParam.page = 1
      this.getList()
    },

    // 分页请求
    tablePageChange(e) {
      this.queryParam.page = e
      this.getList()
    },
    // 获取数据
    async getList() {
      const hide = this.$message.loading('加载中...', 1000)
      const res = await banlanceRecord(this.queryParam)
      if (res.code == 0) {
        this.list = res.data.list
        this.mch = res.data.mch
        this.totalCount = res.data.count
      }
      hide()
    },
  },
}
</script>
<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 15px;
  }
}
.tip {
  line-height: normal;
  margin-bottom: 0;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.45);
}

::v-deep {
  .card_top_padding .ant-card-body {
    padding-bottom: 0;
  }
}

.goods-info {
  display: flex;
  align-items: flex-start;
  position: relative;
  .goods-pic {
    width: 48px;
    height: 48px;
    border-radius: 4px;
    display: block;
  }
  .info-box {
    margin-left: 24px;
    margin-right: 5px;
    .goods-name {
      width: 185px;
      font-size: 14px;
      line-height: 22px;
      color: rgba(0, 0, 0, 0.65);
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
  }
}

.tag {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-right: 4px;
}
.money {
  display: flex;
  flex-direction: column;
  padding-top: 16px;
  div {
    font-size: 26px;
    font-weight: 600;
  }
}
</style>
